<!DOCTYPE html> 
<html> 
	<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<title>jQuery Mobile Docs - Preload & cache pages</title> 
	<link rel="stylesheet" media="only all" href="../../themes/default/" />  
	<link rel="stylesheet" href="../_assets/css/jqm-docs.css"/>
	<script src="../../js/jquery.js"></script>
	<script src="../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
	<script src="../_assets/js/jqm-docs.js"></script>
	<script src="../../js/"></script>
</head> 
<body> 

	<div data-role="page" class="type-interior">

		<div data-role="header" data-theme="f">
		<h1>Preload & cache pages</h1>
		<a href="../../index.html" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
	</div><!-- /header -->

	<div data-role="content">
		<div class="content-primary">		
			
			
			<h2>Pre-fetching pages</h2>
		
			<p>If you're using a single page template, but would prefer to lazy load in a few key pages in the background to avoid seeing the Ajax loader, we recommend using the pre-fetch feature instead of moving to a multi-page template. To pre-fetch a page, simply add the <code>data-prefetch</code> attribute to any link and jQuery Mobile will lazy load this page in the background after the primary page has loaded. Here's an example:</p>
			
<pre><code>
 &lt;a href="foo/bar/baz" data-prefetch&gt;link text&lt;/a&gt;
</code></pre>


			 <p>Technically here's how it works: after pagecreate, jQuery Mobile will automatically find all links in a page that have an attribute of <code>data-prefetch</code> and automatically load the pages so they're available as soon as the user clicks on the link. The Ajax loader won't appear unless the framework hasn't loaded the page by the time the link was clicked.</p> 
			
			<p>Pre-fetching links will naturally cause additional HTTP requests and increased bandwidth that may never be used, so it's important to use this feature only in situations where it's highly likely that a page will be visited.</p>
		
	
<pre><code> &lt;a href="foo/bar/baz" data-prefetch&gt;link text&lt;/a&gt;</code></pre>

		<p>Pages can also be pre-fetched programmatically by calling <code>$.mobile.loadpage( url )</code></p>


		<h2>DOM size management</h2>

		<p>Since animated page transitions require that the page you're on and the one you're transitioning to are both in the DOM, we add pages to the DOM as you navigate around. Before Beta 2, those pages would continue to stay in the DOM until a full page refresh occured so there was always a concern that we could hit a memory ceiling on some devices and cause the browser to slow down or even crash.</p>

		<p>The jQuery Mobile framework has a simple mechanism to keep the DOM tidy: whenever a page is loaded in via Ajax, it is flagged for removal from the DOM once you navigate away to another page (technically, on pagehide). If you return to a deleted page, the browser may be able to retrieve the file from it's cache, or it will re-request it fro the sever if needed. In the case of nested lists, we remove all the pages that make up the nested list once you navigate to a page that's not part of the list. Pages that are included in a multi-page setup won't be affected by this feature at all - only pages brought in by Ajax are managed this way by jQuery Mobile.</p>

		<p>A page option called <code>domCache</code> controls whether to leave pages in the DOM as a way to cache them (the way things used to work) or keep the DOM clean and remove hidden pages (the new way). By default, <code>domCache</code> is set to <code>false</code> to keep the DOM size actively managed. If you set this to <code>true</code>, you need to take care to manage the DOM yourself and test thoroughly on a range of devices.</p>

		<p>Set the domCache option globally like this:
<pre><code>$.mobile.page.prototype.options.domCache = true;</code></pre>

		<p>To set the domCache option on an individual pages, you can add the <code>data-dom-cache="true"</code> attribute to the page container to tell teh framework to not remove the page when it's hidden:</p>
<pre><code>&lt;a href="foo/bar/baz" data-dom-cache="true"&gt;link text&lt;/a&gt;
</code></pre>

		
		
		<p>Alternatively, pages can be cached in the DOM programmatically like this:</p>
<pre><code>elem.page({ domCache: true });</code></pre>




				</div><!--/content-primary -->		

				<div class="content-secondary">

					<div data-role="collapsible" data-collapsed="true" data-theme="b">

							<h3>More in this section</h3>

							<ul data-role="listview" data-theme="c" data-dividertheme="d">

								<li data-role="list-divider">Pages &amp; Dialogs</li>
								<li><a href="page-anatomy.html">Anatomy of a page</a></li>
								<li><a href="page-template.html" data-ajax="false">Single page template</a></li>
								<li><a href="multipage-template.html" data-ajax="false">Multi-page template</a></li>
								<li><a href="page-titles.html">Page titles</a></li>
								<li><a href="page-links.html">Linking pages</a></li>
								<li><a href="page-transitions.html">Page transitions</a></li>
								<li><a href="page-dialogs.html">Dialogs</a></li>
								<li data-theme="a"><a href="page-cache.html">Preload & cache pages</a></li>
								<li><a href="page-navmodel.html">Ajax, hashes &amp; history</a></li></li>
								<li><a href="page-scripting.html">Scripting pages</a></li>
								<li><a href="pages-themes.html">Theming pages</a></li>

							</ul>
					</div>
				</div>		

			</div><!-- /content -->

			<div data-role="footer" class="footer-docs" data-theme="c">
					<p>&copy; 2011 The jQuery Project</p>
			</div>

			</div><!-- /page -->

			</body>
			</html>